package com.runfast.waimai.web.controller.business;

import com.google.common.base.Joiner;
import com.runfast.common.utils.SessionContext;
import com.runfast.common.web.entity.Result;
import com.runfast.waimai.dao.model.*;
import com.runfast.waimai.service.RunfastBusinessService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * ${DESCRIPTION}   商家排名
 *
 * @author chenzheng
 * @create 2018-05-14 17:34
 **/
@RestController("businessRankController")
@RequestMapping("/server/businessRank")
public class BusinessRankController {
    @Autowired
    private RunfastBusinessService runfastBusinessService;
    /**
     * 商家列表
     */
    @GetMapping("/list")
    public Result list(@PageableDefault Pageable pageable, RunfastBusiness business){

        BaseUser admin = SessionContext.getAdmin();
        WaiMaiRunfastAgentaccounts agentAccounts= SessionContext.getCurrentRunfastAgentaccounts();
        Map<Object,Object> map=new HashMap<Object, Object>();
        Map<Object,Object> map1=new HashMap<Object, Object>();
        map1.put("pageable",pageable);
        /**
         * 优先排名的和金牌商家
         * */
        List<RunfastBusiness> list=new ArrayList<RunfastBusiness>();
        Integer total=0;
        /*** 普通商家（不是优先排名的及金牌商家的)*/
        if(business.getId()!=null){
            map1.put("id",business.getId());
        }
        if(StringUtils.isNotBlank(business.getName())){
            map1.put("name",business.getName());
        }
        if(admin!=null && admin.getType()==1){
            //看所有的店铺
            //后台管理员---查看全部的
            if(StringUtils.isNotBlank(business.getAgentIds())){
                map1.put("agentIds",business.getAgentIds());
            }else{
                if(admin.getAgents().size()>0){
                    map1.put("agentIds",Joiner.on(",").join(admin.getAgents()) );
                }
            }
        }else if(admin!=null && admin.getType()==2 && agentAccounts!=null){
            //代理商登录只能看旗下管理的店铺
            if(StringUtils.isNotBlank(business.getAgentIds())){
                map1.put("agentIds",business.getAgentIds());
            }else{
                map1.put("agentIds",agentAccounts.getAgentid());
            }
        }
        if(business.getSelectRank()!=null){
            if(business.getSelectRank()==1){
                //金牌或优先
                map1.put("selectRank",1);
                list=runfastBusinessService.selectRankByMapWithPageable(map1);
                total=runfastBusinessService.countRankByMapWithPageable(map1);
                map.put("list",list);
                map.put("total",total);
            }else if(business.getSelectRank()==2){
                //普通商家
                map1.put("selectRank",2);
                list=runfastBusinessService.selectRankByMapWithPageable(map1);
                total=runfastBusinessService.countRankByMapWithPageable(map1);
                map.put("list",list);
                map.put("total",total);
            }
        }
        if(list.size()>0){
            for(RunfastBusiness business1:list){
                runfastBusinessService.setActivityToBusiness(business1);
            }
        }
        return Result.ok(null,map);
    }


    /**
     * 普通商家列表
     */
    /*@GetMapping("/generallist")
    public Result generalList(@PageableDefault Pageable pageable, RunfastBusiness business){

        BaseUser admin = SessionContext.getAdmin();
        Map<Object,Object> map=new HashMap<Object, Object>();
        *//**
         * 普通商家（不是优先排名的及金牌商家的）
         * *//*
        List<RunfastBusiness> generalList=null;
        Integer generalTotal=null;
        RunfastBusinessExample example1=new RunfastBusinessExample();
        RunfastBusinessExample.Criteria example1Criteria=example1.createCriteria();
        if(business.getId()!=null){example1Criteria.andIdEqualTo(business.getId());}
        if(business.getName()!=null){example1Criteria.andNameLike(business.getName());}
        if(business.getAgentid()!=null){example1Criteria.andAgentidEqualTo(business.getAgentid());}
        example1Criteria.andStatusxNotEqualTo(-1);
        if(admin!=null && admin.getType()==1){
            //看所有的店铺
            //后台管理员---查看全部的
            if(admin.getAgents().size()>0){
                if(business.getAgentid()==null){
                    example1Criteria.andAgentidIn(admin.getAgents());
                }
            }
        }else if(admin.getType()==2){
            //代理商登录只能看旗下管理的店铺
            WaiMaiRunfastAgentaccounts agentAccounts= SessionContext.getCurrentRunfastAgentaccounts();
            if(agentAccounts.getAgentid() !=null){
                example1Criteria.andAgentidEqualTo(agentAccounts.getAgentid());
            }
        }
        //普通商家
        example1Criteria.andPriorityEqualTo(false);
        example1Criteria.andGoldbusinessEqualTo(false);
        example1.or().andExpireTimeLessThan(new Date());
        generalList=runfastBusinessService.selectByExampleWithPageable(example1,pageable);
        generalTotal=runfastBusinessService.countByExample(example1);
        map.put("generalList",generalList);
        map.put("generalTotal",generalTotal);
        return Result.ok(null,map);
    }*/


    //15天后过期
    private final static int EXPIRE1 = 3600 * 24 * 15;
    /**
     *设置金牌商家
     * */
    @GetMapping("/setGoldBusiness")
    public Result setGoldBusiness(Integer id){
        //当前时间
        Date now = new Date();
        //过期时间
        Date expireTime = new Date(now.getTime() + EXPIRE1 * 1000);

        RunfastBusiness  business=runfastBusinessService.selectByPrimaryKey(id);
        if(business.getAgentid()!=null){
            RunfastBusinessExample example=new RunfastBusinessExample();
            example.createCriteria().andAgentidEqualTo(business.getAgentid()).andStatusxNotEqualTo(-1);
            Integer total=runfastBusinessService.countByExample(example);
            if(total>0){
                RunfastBusinessExample example1=new RunfastBusinessExample();
                example1.createCriteria().andAgentidEqualTo(business.getAgentid()).andStatusxNotEqualTo(-1).andGoldbusinessEqualTo(true);
                Integer total1=runfastBusinessService.countByExample(example1);
                if(Double.parseDouble(total1.toString())/Double.parseDouble(total.toString())<0.08){
                    business.setGoldbusiness(true);
                    business.setExpireTime(expireTime);
                    runfastBusinessService.updateByPrimaryKeySelective(business);
                }else{return Result.fail(null,"金牌商家数量不能大于该区域有效商家数量的8%");}
            }
        }
        return  Result.ok(null);
    }
    /**
     * 设置优先排名
     * */
    @GetMapping("/setPriorityBusiness")
    public Result setPriorityBusiness(Integer id){
        RunfastBusiness  business=runfastBusinessService.selectByPrimaryKey(id);
        if(business.getAgentid()!=null){
            RunfastBusinessExample example=new RunfastBusinessExample();
            example.createCriteria().andAgentidEqualTo(business.getAgentid()).andStatusxNotEqualTo(-1);
            Integer total=runfastBusinessService.countByExample(example);
            if(total>0){
                RunfastBusinessExample example1=new RunfastBusinessExample();
                example1.createCriteria().andAgentidEqualTo(business.getAgentid()).andStatusxNotEqualTo(-1).andPriorityEqualTo(true);
                Integer total1=runfastBusinessService.countByExample(example1);
                if(Double.parseDouble(total1.toString())/Double.parseDouble(total.toString())<0.07){
                    business.setPriority(true);
                    runfastBusinessService.updateByPrimaryKeySelective(business);
                }else{return Result.fail(null,"优先排名商家数量不能大于该区域有效商家数量的7%!");}
            }
        }
        return Result.ok(null);
    }

    /**
     * 取消金牌商家
     * */
    @GetMapping("/cancelGoldBusiness")
    public Result cancelGoldBusiness(Integer id){
        RunfastBusiness  business=runfastBusinessService.selectByPrimaryKey(id);
        if(business !=null){
            business.setGoldbusiness(false);
            runfastBusinessService.updateByPrimaryKeySelective(business);
        }
    return  Result.ok(null);
    }

    /**
     * 取消优先排名
     * */
    @GetMapping("/cancelPriorityBusiness")
    public Result cancelPriorityBusiness(Integer id){
        RunfastBusiness  business=runfastBusinessService.selectByPrimaryKey(id);
        if(business !=null){
            business.setPriority(false);
            runfastBusinessService.updateByPrimaryKeySelective(business);
        }
        return  Result.ok(null);
    }

}
